package com.sgzn.sgpay.persistence.manager;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.exception.SgpayException;
import com.sgzn.sgpay.persistence.dao.TLoginSessionMapper;
import com.sgzn.sgpay.persistence.domain.TLoginSession;
import com.sgzn.sgpay.persistence.domain.TLoginSessionExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author huangsenguang
 * @version Id: LoginSessionManger.java, v 0.1 2018/4/18 21:44 huangsenguang Exp $$
 * 用户登录Session管理,不是商户的
 */
@Slf4j
@Component
public class LoginSessionManger {
    @Autowired
    private TLoginSessionMapper tLoginSessionMapper;

    public TLoginSession gettLoginSession(String loginToken) {
        log.info("loginToken:{}", loginToken);
        TLoginSessionExample queryExample = new TLoginSessionExample();
        TLoginSessionExample.Criteria criteria = queryExample.createCriteria();
        criteria.andTokenEqualTo(loginToken);
        List<TLoginSession> sList = tLoginSessionMapper.selectByExample(queryExample);
        if(CollectionUtils.isEmpty(sList)){
            throw new SgpayException(SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseCode(),SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseDesc());
        }
        return sList.get(0);
    }
}

